package com.broadvision.bvgs.multilang.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.broadvision.bvgs.multilang.MultilangUtils;
import com.broadvision.data.client.Content;
import com.broadvision.data.common.RowData;
import com.broadvision.data.common.TableData;
import com.broadvision.portal.services.PortalVisitor;
import com.broadvision.portal.tools.action.BaseAction;

// Referenced classes of package com.broadvision.portal.tools.action.qualifier:
//            QualifierUtils

public abstract class IMultilangValueObjectMultilangViewAction extends BaseAction
{

    public ActionForward processExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
    	
        if (request.getParameter("channelId")!=null){
        	
        	if (!(request.getParameter("channelId").equalsIgnoreCase("0"))){
    	
    	System.out.println("inside class3: "+ this.getClass());
    	
    	
        String catOid = (String)PropertyUtils.getSimpleProperty(form, "catOid");
        String cntTypeId = (String)PropertyUtils.getSimpleProperty(form, "cntTypeId");
    	
    	
        System.out.println("catOid: "+catOid);
        System.out.println("cntTypeId: "+cntTypeId);
    	
        
        
        
    	
    	
    	
        int serviceId = getServiceId(request);
        System.out.println("inside class3-1");
        PortalVisitor portalVisitor = getPortalVisitor(request);
        System.out.println("inside class3-2");
        Content content = getContentToTranslate(request);
        System.out.println("inside class3-3");
        prepareFormBean(form, request, content);
        System.out.println("inside class3-4");
        prepareViewBeans(mapping, request, serviceId, portalVisitor, content);
        System.out.println("inside class3-5");
        	}
        }
        
        
        return mapping.findForward("success");
    }

    protected void prepareFormBean(ActionForm actionform, HttpServletRequest httpservletrequest, Content content)
        throws Exception
    {
    }

    protected void prepareViewBeans(ActionMapping mapping, HttpServletRequest request, int serviceId, PortalVisitor portalVisitor, Content content)
        throws Exception
    {
        TableData translationTable = getTranslationList(serviceId, content);
        List translationList = new ArrayList();
        request.setAttribute("translations", translationList);
        String languageField = getTranslationLanguageFieldName();
        String nameField = getTranslationNameFieldName();
        
        for(int index = 0; index < translationTable.size(); index++)
        {
        	RowData translationItem = translationTable.getRow(index);
        	String language = translationItem.getStringValue(languageField);
        	String translatedName = translationItem.getStringValue(nameField);
        	com.broadvision.web.util.ViewDynaBean dto = MultilangUtils.prepareMultilangNameBean(mapping, language, translatedName);
        	translationList.add(dto);
         }
    }
    
    protected long getClientId(HttpServletRequest request)
    throws Exception
{
    PortalVisitor portalVisitor = getPortalVisitor(request);
    return portalVisitor.getId();
}

    protected TableData getTranslationList(int serviceId, Content content){
		return MultilangUtils.getTranslationList(content, getTranslationTableName());
    }
    
   protected abstract Content getContentToTranslate(HttpServletRequest httpservletrequest)
   		throws Exception;
    
   protected abstract String getTranslationTableName();
   protected abstract String getTranslationLanguageFieldName();
   protected abstract String getTranslationNameFieldName();
}